package project1;

import java.util.ArrayList;
import java.util.List;

/**
 *  @author Andrew O'Connor
 *  COSC 603
 *  Project 1
 */
public class FindRepeatedWords {
    
    private List<String> clean_input;
    private List<RepeatedWord> repeats;

    public FindRepeatedWords(List<String> _clean_input) {
        clean_input = _clean_input;
        repeats = new ArrayList<RepeatedWord>();
    }

    /**
     *
     * finds the repeats in clean_input
     */
    public void findRepeats() {

        repeats = new ArrayList<RepeatedWord>();

        for (int i = 0; i < clean_input.size(); i++) {
            String[] line = clean_input.get(i).split("\\s+");
            String[] next_line = {};
            if (i + 1 < clean_input.size())
                next_line = clean_input.get(i + 1).split("\\s+");

            for (int j = 0; j < line.length; j++) {
                String word = line[j];
                String next_word = "";
                int lineNumber = i + 1;
                if (j + 1 < line.length)
                    next_word = line[j + 1];
                if (j + 1 == line.length) {
                    if (next_line.length > 0) {
                        next_word = next_line[0];
                        lineNumber++;
                    }
                }
                if (word.equals(next_word))
                    repeats.add(new RepeatedWord(word, lineNumber));
            }
        }
    }

    /**
     *
     * prints the list of repeated words
     */
    public void printRepeats() {
        for (RepeatedWord repeat : repeats)
            System.out.println(repeat.toString());
    }
}
